package ee.siimkaspar.subjects.controller;

import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import ee.siimkaspar.subjects.model.Address;
import ee.siimkaspar.subjects.model.Contact;
import ee.siimkaspar.subjects.model.ModelInterface;
import ee.siimkaspar.subjects.model.Person;
import ee.siimkaspar.subjects.model.SubjectAttribute;
import ee.siimkaspar.subjects.model.SubjectAttributeType;
import ee.siimkaspar.subjects.model.SubjectType;
import ee.siimkaspar.subjects.service.SearchService;
import ee.siimkaspar.subjects.service.SubjectService;

public class SearchAction extends BaseAction {

	private List<SubjectType> allSubjectTypes;
	private Integer subjectType;
	private String redirectSearchType;
	
	private Person person;
	private List<Person> resultListPerson;
	
	private Address address;
	private Contact contact;
	private List<SubjectAttribute> attributes;
	
	private SearchService searchService;
	private SubjectService subjectService;
	
	public SearchAction(SearchService sService, SubjectService subjectService) {
		this.searchService = sService;
		this.subjectService = subjectService;
	}
	
	@Override
	public void prepare() throws Exception {
		super.prepare();
		System.out.println(subjectType);
		allSubjectTypes = subjectService.getAllSubjectTypes();
	}
	
	private void getAllAttributes() {
		List<SubjectAttributeType> attributeTypes = subjectService.getAllSubjectAttributeTypes(subjectType);
		if (attributes == null || attributes.isEmpty()) {
			System.out.println("SIIA=" + attributes);
			attributes = new ArrayList<SubjectAttribute>();
			for (SubjectAttributeType attributeType : attributeTypes) {
				attributes.add(new SubjectAttribute(attributeType));
			}
		}
	}
	
	public String searchSelect() {
		return SUCCESS;
	}
	
	public String showSearchForm() {
		switch (subjectType) {
		case EditSubjectAction.PERSON:
			redirectSearchType = "showPersonSearch";
			break;
		case EditSubjectAction.ENTERPRISE:
			redirectSearchType = "showEnterpriseSearch";
			break;
		case EditSubjectAction.EMPLOYEE:
			redirectSearchType = "showEmployeeSearch";
			break;
		case EditSubjectAction.CUSTOMER:
			redirectSearchType = "showCustomerSearch";
			break;
		default:
			break;
		}
		return SUCCESS;
	}
	
	public String loadSearchForm() {
		if (subjectType == null) {
			Logger.getLogger(getClass()).error("Invalid parameter(s)");
			throw new InvalidParameterException();
		}
		if (subjectType != null) {
			getAllAttributes();
		}
		return SUCCESS;
	}
	
	public String searchPerson() {
		resultListPerson = searchService.searchPersons(person, address, contact, attributes);
		return SUCCESS;
	}
	
	public String searchEnterprise() {
		
		return SUCCESS;
	}
	
	public String searchEmployee() {
		
		return SUCCESS;
	}
	
	public String searchCustomer() {
		
		return SUCCESS;
	}
	//--------------------- getter and setters --------------------------------//
	public List<SubjectType> getAllSubjectTypes() {
		return allSubjectTypes;
	}

	public void setAllSubjectTypes(List<SubjectType> allSubjectTypes) {
		this.allSubjectTypes = allSubjectTypes;
	}

	public Integer getSubjectType() {
		return subjectType;
	}

	public void setSubjectType(Integer subjectType) {
		this.subjectType = subjectType;
	}

	public String getRedirectSearchType() {
		return redirectSearchType;
	}

	public void setRedirectSearchType(String redirectSearchType) {
		this.redirectSearchType = redirectSearchType;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public List<Person> getResultListPerson() {
		return resultListPerson;
	}

	public void setResultListPerson(List<Person> resultListPerson) {
		this.resultListPerson = resultListPerson;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public Contact getContact() {
		return contact;
	}

	public void setContact(Contact contact) {
		this.contact = contact;
	}

	public List<SubjectAttribute> getAttributes() {
		return attributes;
	}

	public void setAttributes(List<SubjectAttribute> attributes) {
		this.attributes = attributes;
	}

}
